/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.state;

import deatax.GameData;
import deatax.Improvement;
import deatax.Player;
import deatax.Tile;
import deatax.display.gui.ContextItem;
import deatax.network.Client;
import deatax.network.Messages;
import deatax.network.NetworkListener;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;

/**
 *
 * @author Pieman
 */
public class PlacementState extends GameState implements NetworkListener {

    PlacementMouseListener listener;
    protected JPopupMenu context_menu;
    Tile selected;
    boolean allow_control;

    public PlacementState(GameData data) {
        super("Placement", data);
    }

    @Override
    public void begin() {
        allow_control = data.getCurrentPlayer() == data.getSelf() || data.getSelf() == null;
        data.getGui().enableEndTurn(false);
        data.getGui().setPlayerInfo(data.getCurrentPlayer());
        data.getGui().refreshContent();
        selected = null;
        listener = new PlacementMouseListener();
        context_menu = new JPopupMenu();
        data.getDisplay().addMouseListener(listener);
        Client.get().addListener(this);
    }

    @Override
    public void die() {
        if (listener != null) {
            data.getDisplay().removeMouseListener(listener);
        }
        Client.get().removeListener(this);
    }

    @Override
    public void finish() {
        data.getMap().setSelectedTile(null);
        data.getGui().setTileInfo(null);
        data.nextPlayerTurn();
        int towns = data.getCurrentPlayer().getImprovementNum(data.getGame().getBaseSettlement());
        data.getState().popState();
        if (towns > 0) {
            data.getState().pushState("Roll");
        } else {
            data.getState().pushState("Placement");
        }
    }

    public void createContextMenu(int x, int y) {
        Tile tile = data.getRender().getHover();
        selected = tile;
        context_menu.removeAll();
        Player p = data.getCurrentPlayer();
        if (tile != null && tile.getTerrain() != null) {
            if (tile.getOwner() == null) {
                if (tile.getTerrain().getImprovements().contains(data.getGame().getImprove("Road"))) {
                    boolean allow = true;
                    for (Tile neigh : tile.getNeighbours()) {
                        if (neigh.getOwner() != null) {
                            allow = false;
                            break;
                        }
                    }
                    if (allow) {
                        ContextItem item = new ContextItem("Place here");
                        item.addActionListener(new PlacementActionListener());
                        context_menu.add(item);
                        context_menu.addSeparator();
                        ContextItem cancel = new ContextItem("Cancel");
                        context_menu.add(cancel);
                    } else {
                        ContextItem item = new ContextItem("Too close to another player");
                        context_menu.add(item);
                    }
                } else {
                    ContextItem item = new ContextItem("Cannot place on this terrain");
                    context_menu.add(item);
                }
            } else {
                ContextItem item = new ContextItem("This tile belongs to another player");
                context_menu.add(item);
            }
        }
        if (context_menu.getComponentCount() > 0) {
            //context_menu.setLocation(MouseInfo.getPointerInfo().getLocation());
            int max_width = 0;
            int height = 0;
            for (Component c : context_menu.getComponents()) {
                Dimension d = c.getPreferredSize();
                if (d.width > max_width) {
                    max_width = d.width;
                }
                height += d.height;
            }
            context_menu.setPopupSize(new Dimension(max_width, height));
            context_menu.show(data.getDisplay(), x, y);
        }
    }

    protected class PlacementMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
                Tile t = data.getRender().getHover();
                data.getMap().setSelectedTile(t);
                if (t == null) {
                    selected = null;
                    return;
                }
                if (allow_control) {
                    createContextMenu(e.getX(), e.getY());
                }
            } else if (e.getButton() == MouseEvent.BUTTON1) {
                Tile t = data.getRender().getHover();
                data.getMap().setSelectedTile(t);
                data.getGui().setTileInfo(t);
            }

        }
    }

    protected class PlacementActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            Tile t = selected;
            if (t != null) {
                Player p = data.getCurrentPlayer();
                Client.get().sendMessage(Messages.CLAIM, new String[]{p.getName(), String.valueOf(t.getX()), String.valueOf(t.getY())});
                //t.setOwner(p);
                Client.get().sendMessage(Messages.BUILD, new String[]{data.getGame().getBaseSettlement().getName(), String.valueOf(t.getX()), String.valueOf(t.getY())});
                //t.setImprovement(data.getGame().getBaseSettlement());
                Client.get().sendMessage(Messages.RESOURCE, new String[]{p.getName(), "Money", "100"});
                Client.get().sendMessage(Messages.UNIT, new String[]{p.getName(), "Worker", "3", String.valueOf(t.getX()), String.valueOf(t.getY())});
                Client.get().sendMessage(Messages.LOG, new String[]{p.getName(), "Placed their starting town and receives $100 and 3 workers."});
                Client.get().sendMessage(Messages.END_TURN);
                //data.getGui().addMessage(p, "Placed their starting town and receives $200 and 3 workers.");
                //p.addResource(data.getGame().getResource("Money"), 200);
                //t.addUnit(p, data.getGame().getUnit("Worker"), 3);
                //finish();
            }

        }
    }

    @Override
    public void processMessage(int type, String[] arg) {
        commonMessages(type, arg);
    }
}
